package com.weight.system.login.web;

import com.weight.system.login.PO.LoginInfo;
import com.weight.system.login.VO.LoginInfoVO;
import com.weight.system.login.service.LoginInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 登录日志表
 */
@RestController
@Slf4j
@RequestMapping("/first")
public class LoginInfoController {

	@Autowired
	private LoginInfoService loginInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询登录日志表--列表页 
	 * @return
	 */
	@RequestMapping(value="/firstmessage",method=RequestMethod.GET)
	public ModelAndView queryForLoginInfoList(){
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/login/queryForLoginInfoList");
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 2.查询登录日志表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForLoginInfoListDG",method=RequestMethod.GET)
	public String queryForLoginInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String operId = request.getParameter("operId");
			String logDate = request.getParameter("logDate");
			String logIp = request.getParameter("logIp");
			String loginLon = request.getParameter("loginLon");
			String loginLat = request.getParameter("loginLat");

			Map<Object, Object> map =new HashMap<>();
			map.put("operId", operId);
			map.put("logDate", DateFormUtils.getStringToDate(logDate, pattern));
			map.put("logIp", logIp);
			map.put("loginLon", loginLon);
			map.put("loginLat", loginLat);

			
			//2、查询登录日志表列表
			List<LoginInfo> loginInfoList = loginInfoService.findLoginInfoByPage(page,rows,map);
			log.info("loginInfoList={}",loginInfoList);
			List<LoginInfoVO> loginInfoVOList = loginInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<LoginInfoVO> result = new PageResult<LoginInfoVO>(loginInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param loginInfoVO
	 * @param loginInfo
	 * @return
	 */
	public void changeVOToPO(LoginInfoVO loginInfoVO,LoginInfo loginInfo){
			BeanUtils.copyProperties(loginInfoVO,loginInfo);
			loginInfo.setLogDate(DateFormUtils.getStringToDate(loginInfoVO.getLogDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param loginInfoVO
	 * @param loginInfo
	 * @return
	 */
	public void changePoToVO(LoginInfoVO loginInfoVO,LoginInfo loginInfo){
			BeanUtils.copyProperties(loginInfo,loginInfoVO);
			loginInfoVO.setLogDate(DateFormUtils.getDateToString(loginInfo.getLogDate(), pattern));
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param loginInfo
	 * @return
	 */
	public LoginInfoVO changePoToVO(LoginInfo loginInfo){
			LoginInfoVO loginInfoVO=new LoginInfoVO();
			BeanUtils.copyProperties(loginInfo,loginInfoVO);
			loginInfoVO.setLogDate(DateFormUtils.getDateToString(loginInfo.getLogDate(), pattern));
			

			return loginInfoVO;
	}

	/**
	 * 3.查询登录日志表--详情页
	 * @param logInfoId
	 * @return
	 */
	@RequestMapping("/queryForLoginInfoDetail")
	public ModelAndView queryForLoginInfoDetail(@RequestParam(value="logInfoId",required=false) String logInfoId){
			//1.校验id
			CommonUtils.validateExistID(logInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("logInfoId", logInfoId);
			log.info("查询登录日志表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			LoginInfo loginInfo = loginInfoService.findByLoginInfoId(map);
			
			LoginInfoVO loginInfoVO = changePoToVO(loginInfo);
			//3、返回前台
			log.info("查询登录日志表查询成功：loginInfo={}",loginInfo);
			modelAndView.setViewName("/system/login/queryForLoginInfoDetail");
			modelAndView.addObject("loginInfoVO",loginInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改登录日志表操作
	 * @param logInfoId
	 * @return
	 */
	@RequestMapping(value="/addLoginInfo")
	public ModelAndView addLoginInfo(@RequestParam(value="logInfoId",required=false) String logInfoId){
			//1、创建返回对象
			LoginInfoVO loginInfoVO = new LoginInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(logInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("logInfoId", logInfoId);
				log.info("修改操作，查询ID：logInfoId={}",logInfoId);
				LoginInfo loginInfo = loginInfoService.findByLoginInfoId(map);
				this.changePoToVO(loginInfoVO,loginInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("/system/login/addLoginInfo");
			modelAndView.addObject("loginInfoVO",loginInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改登录日志表操作
	 * @param loginInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveLoginInfo")
	public JsonResultUtil saveLoginInfo(@Valid LoginInfoVO loginInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,loginInfoVO);
			
			//2.获取id
			String logInfoId=loginInfoVO.getLogInfoId();
			LoginInfo loginInfo=new LoginInfo();
			BeanUtils.copyProperties(loginInfoVO,loginInfo);
			this.changeVOToPO(loginInfoVO,loginInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(logInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("logInfoId", logInfoId);
				log.info("修改操作，查询ID：logInfoId={}",logInfoId);
				//查询是否存在
				loginInfoService.findByLoginInfoId(map);
				try{
					loginInfo.setUpdateBy("000");
					loginInfoService.updateLoginInfo(loginInfo);
					log.info("更新操作成功：loginInfo={}",loginInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					loginInfo.setLogInfoId(CommonUtils.getUUId());
					loginInfo.setCreateBy("000");
					loginInfo.setUpdateBy("000");
					loginInfoService.insertLoginInfo(loginInfo);
					log.info("添加操作成功：loginInfo={}",loginInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除登录日志表操作
	 * @param logInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByLoginInfoId")
	public JsonResultUtil deleteByLoginInfoId(@RequestParam(value="logInfoId",required=false) String logInfoId){
			//1.校验id
			CommonUtils.validateExistID(logInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("logInfoId", logInfoId);
			loginInfoService.findByLoginInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除登录日志表入参，map={}",map);
			
			try{
				loginInfoService.upDelByLoginInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除登录日志表操作---批量删除
	 * @param logInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByLoginInfoIdBatch")
	public JsonResultUtil deleteByLoginInfoIdBatch(@RequestParam(value="logInfoIdList",required=false) List logInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(logInfoIdList);
			
			//2.校验数据库
			loginInfoService.findByLoginInfoIdList(logInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("logInfoIdList", logInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除登录日志表操作入参，map={}",map);
			
			try{
				loginInfoService.upDelByLoginInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}